package com.easylinkin.linkappapi.alarm.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 告警通知日志
 * </p>
 *
 * @author TongJie
 * @since 2020-05-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("linkapp_alarm_notice_log")
public class AlarmNotice extends Model<AlarmNotice> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId("id")
    private String id;

    /**
     * 告警id(关联linkapp_alarm)
     */
//    @TableField("alarm_rule_id")
//    private String alarmRuleId;

    /**
     * 规则引擎id
     */
    @TableField("rule_engine_id")
    private String ruleEngineId;

    /**
     * 告警id
     */
    @TableField("alarm_id")
    private String alarmId;

    /**
     * 订阅人id (关联linkapp_user)
     */
    @TableField("user_id")
    private String userId;

    /**
     * 告警内容
     */
    @TableField("content")
    private String content;

    /**
     * 发送状态；0发送失败，1发送成功
     */
    @TableField("status")
    private Integer status = 0;

    /**
     * 发送时间
     */
    @TableField("send_time")
    private Date sendTime;

//    @TableField(exist = false)
//    private AlarmRule alarmRule;


    @TableField(exist = false)
    private LinkappUser linkappUser;

    /**
     * 规则引擎名称
     */
    @TableField(exist = false)
    private String ruleEngineName;

    /**
     * 消息推送类型 1-短信 2-邮件
     */
    @TableField("message_type")
    private Integer messageType;


    /**
     * 消息方式
     */
    @TableField("message_way")
    private String messageWay;


    /**
     * 告警方式(电话号码)
     */
    @TableField(exist = false)
    private String phoneNumber;


    /**
     * 邮箱地址(电话号码)
     */
    @TableField(exist = false)
    private String emailAddress;

    /**
     * 告警模板（短信：阿里预留的模板id  邮件：自定义的邮件模板）
     */
    @TableField(exist = false)
    private String templetId;

    /**
     * 告警模板（短信）
     */
    @TableField(exist = false)
    private String templetMessageContent;


    /**
     * 告警模板（邮箱）
     */
    @TableField(exist = false)
    private String templeteMailContent;


    /**
     * 设备类型
     */
    @TableField(exist = false)
    private String deviceType;

    /**
     * 设备名称
     */
    @TableField(exist = false)
    private String deviceName;

    /**
     * 区域位置
     */
    @TableField(exist = false)
    private String areaPath;

    /**
     * 设备code
     */
    @TableField(exist = false)
    private String deviceCode;

    /**
     * 租户tenantId
     */
    @TableField(exist = false)
    private String tenantId;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    public String getEmailBodyText(Template template) throws IOException, TemplateException {
        Map<String, String> param = new HashMap<>(10);
        param.put("areaPath", this.getAreaPath());
        param.put("deviceType", this.getDeviceType());
        param.put("content", this.getContent());
        StringWriter writer = new StringWriter();
        template.process(param, writer);
        return writer.toString();
    }
    

}
